<form id="addIdenManageForm" lay-filter="editGroupForm" class="layui-form model-form">
    <div class="model-form-body" style="max-height: 550px;">
        <div class="layui-form-item">
            <label class="layui-form-label">名称</label>
            <div class="layui-input-block">
                <input name="name" placeholder="请输入名称" type="text" class="layui-input" maxlength="20"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">编码</label>
            <div class="layui-input-block">
                <input name="code" placeholder="请输入编码" type="text" class="layui-input" maxlength="20"
                       lay-verType="tips" lay-verify="" />
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">对象分类</label>
            <div class="layui-input-block">
                <select name="metaType" placeholder="请选择对象分类" id="metaType">
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">报告期</label>
            <div class="layui-input-block">
                <select name="reportType" placeholder="请选择报告期" id="reportType">
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">报送开始时间</label>
            <div class="layui-input-block">
                <input type="text" name="startTime" class="layui-input" id="startTime" placeholder="yyyy-MM-dd HH:mm:ss">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">报送结束时间</label>
            <div class="layui-input-block">
                <input type="text" name="endTime" class="layui-input" id="endTime" placeholder="yyyy-MM-dd HH:mm:ss">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">报送地点</label>
            <div class="layui-input-block">
                <input name="localeOfSubmission" placeholder="请输入报送地点" type="text" class="layui-input" maxlength="20"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">所属目录</label>
            <div class="layui-input-block">
                <input name="menuName" id="edit-menu" placeholder="" type="text" class="layui-input" maxlength="20"
                       lay-verType="tips" lay-verify="required" disabled/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">选择上级目录</label>
            <div class="layui-input-block">
                <ul id="edit-tree" class="ztree" ></ul>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">是否支持多版本数据导入</label>
            <div class="layui-input-block">
                <input type="checkbox" name="datavIsMulti" value="status" lay-skin="switch" lay-text="是|否">
            </div>
        </div>
    </div>
    <div class="layui-form-item text-right model-form-footer">
        <button class="layui-btn layui-btn-primary layui-btn-sm" type="button" ew-event="closePageDialog">取消</button>
        <button class="layui-btn layui-btn-sm" lay-filter="update-index-class" lay-submit >确定</button>
    </div>
</form>
<!--渲染对象分类-->
<script type="text/tmpl" id="template-meta_type-tmpl">
    <option value=""></option>
     {{each meta_type}}
        <option value="{{$value.value}}">{{$value.meta_type}}</option>
    {{/each}}

</script>
<!--渲染报告期分类-->
<script type="text/tmpl" id="template-report_type-tmpl">
    <option value=""></option>
     {{each report_type}}
        <option value="{{$value.value}}">{{$value.report_type}}</option>
    {{/each}}

</script>
<script>
    layui.use(['layer', 'form', 'admin','tree','laydate'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var admin = layui.admin;
        var tree = layui.tree;
        var laydate = layui.laydate;
        var userInfo = getAuthority();
        var mUser = window.mSelUser;  // 列表页面传递的数据，也可以使用top.xxx
        var editParentId;
        var Authorization;
        var editSetting={//树设置
            identValueSetting:{//区划树设置
                edit: {
                    enable: true,
                    showRemoveBtn: false,
                    showRenameBtn: false,
                    drag: {
                        isCopy: true,
                        isMove: false,
                        prev: false,
                        next: false,
                        inner: false
                    }
                },
                data: {
                    key: {
                        children: "sub",
                        isParent: "isParent",
                        title: "name"
                    },
                    simpleData: {
                        enable: true
                    }
                },
                callback: {
                    onExpand: onEditValueTree,
                    onClick: editValueOnClick
                },
                view: {
                    showIcon: true,
                    showLine: true
                }
            },
        }
        var meta_type;
        var menuId;
        meta_type = mUser.meta_type
        var str = template("template-meta_type-tmpl",{meta_type});
        document.getElementById("metaType").innerHTML = str;

        var report_type;
        report_type = mUser.report_type
        var str = template("template-report_type-tmpl",{report_type});
        document.getElementById("reportType").innerHTML = str
        form.render('select')
        console.log(' mUser.micro_data:', mUser.micro_data)
        if(mUser.micro_data.menuName == null){
            mUser.micro_data.menuName = "根目录";
        }
        form.val('editGroupForm', mUser.micro_data);  // 回显数据
        //日期时间选择器
        laydate.render({
            elem: '#startTime'
            ,type: 'datetime'
        });

        laydate.render({
            elem: '#endTime'
            ,type: 'datetime'
        });
        if(userInfo){
            var access_token = localStorage.getExpire("access_token");
            var token_type = localStorage.getExpire("token_type");
            Authorization = token_type+" "+access_token;
//            getEditTree({},function (resArr) {
//                $.fn.zTree.init($("#edit-tree"), editSetting.identValueSetting, resArr);
//            });
            initEditTree();
        }
        function  initEditTree() {
            var resArr = [{"data_type":0,
                "id":"root",
                "name":"根目录",
                "par_id":"",
                "version":"0",
                "sub":[]
            }];
            $.fn.zTree.init($("#edit-tree"), editSetting.identValueSetting, resArr);
        }
        //获取树形目录的值
        function getEditTree(obj,callback) {
            let parent_id = obj.id?obj.id:'root';
            axios({
                method: 'get',
                url:microUrl.microObject.microMenu.microMenuList+'/'+ parent_id + '/',
                headers:{
                    'Authorization':Authorization
                }
            })
                .then(function (response) {
                    if(response.status == 200 &&response.data.message == "success"){
                        var data = response.data.data;
                        var resArr = [];
                        zhuanHuanTree(data,resArr);
                        // IdentValueTree = resArr;
                        callback(resArr)

                    }
                })
                .catch(function (error) {
                    console.log(error);
                    if(error.response.status === 401 || error.response.status === 403 || error.response.status === 406){
                        // 进行重新登陆
                        localStorage.clear();
                        top.location.href=loginPageUrl;
                    }
                });
        }
        //转换菜单集合数据
        function zhuanHuanTree(tdata,resData) {
            if(Array.isArray(tdata) && tdata.length>0){
                tdata.forEach(function(v,i){
                    var newValue=v.name;
                    resData[i]=v;
                    resData[i].title=newValue;
                    resData[i].icon = "";
                    resData[i].isParent = "true";
                    resData[i].sub = [];
                });
            }
        }

        //查询下方区划树的下级区划值（懒加载）
        function onEditValueTree(event, treeId, treeNode) {
            if(treeNode.sub.length>0||treeNode.hasChild=='0'){
                return false
            }
            getEditTree(treeNode,function (resArr) {
                const treeObj = $.fn.zTree.getZTreeObj("edit-tree");
                treeNode = treeObj.addNodes(treeNode,resArr);


            });
        }
        //选中分组值
        function editValueOnClick(event, treeId, treeNode){
            editParentId = treeNode.id
            $('#edit-menu').val(treeNode.name)
        }

        form.render('select')
        form.render('checkbox')
        // 表单提交事件
        form.on('submit(update-index-class)', function (data) {
            mUser.micro_data.name = data.field.name
            mUser.micro_data.code = data.field.code
            mUser.micro_data.metaType = data.field.metaType
            mUser.micro_data.reportType = data.field.reportType
            mUser.micro_data.startTime = data.field.startTime
            mUser.micro_data.endTime = data.field.endTime
            mUser.micro_data.localeOfSubmission = data.field.localeOfSubmission
            mUser.micro_data.datavIsMulti = data.field.datavIsMulti?1:0
            mUser.micro_data.status = data.field.status?1:0
            mUser.micro_data.menuId = editParentId
            axios({
                method: 'put',
                data:mUser.micro_data,
                url:microUrl.microObject.microList.editMicroList,
                headers:{
                    'Authorization':Authorization
                }
            })
                .then(function (res) {
                    if (res.status == 200&&res.data.message=="success") {
                        layer.closeAll('loading');
                        layer.msg("编辑成功", {icon: 1});
                    } else {
                        layer.msg(res.data.message, {icon: 2});
                        layer.closeAll('loading');

                    }
                    admin.closeDialog('#addIdenManageForm');
                })
                .catch(function (error) {
                    console.log(error);
                    if(error.response.status === 401 || error.response.status === 403 || error.response.status === 406){
                        // 进行重新登陆
                        localStorage.clear();
                        top.location.href=loginPageUrl;
                    }
                })
            return false;

        });

    });
</script>